package com.eneat.smartrule.restAPI;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class AbstractController {

    /**
     * 默认成功返回
     *
     */
    protected <T> HttpResult<T> responseOK(T data) {
        HttpResult<T> restResult = new HttpResult<T>();
        restResult.setSuccess(true);
        restResult.setData(data);
        restResult.setCode(ResultCode.SUCCESS.getCode());
        restResult.setMessage(ResultCode.SUCCESS.getMessage());
        log.info(restResult.toString());
        return restResult;
    }

    /**
     * 默认成功返回带消息
     *
     * @param msg
     * @return
     */
    protected <T> HttpResult<T> responseOK(T data, String msg) {
        HttpResult<T> restResult = new HttpResult<T>();
        restResult.setSuccess(true);
        restResult.setData(data);
        restResult.setCode(ResultCode.SUCCESS.getCode());
        restResult.setMessage(msg);
        return restResult;
    }

    /**
     * 默认成功返回带消息
     *
     * @param msg
     * @return
     */
    protected <T> HttpResult<T> responseOK(String msg) {
        HttpResult<T> restResult = new HttpResult<T>();
        restResult.setSuccess(true);
        restResult.setData( (T)msg );
        restResult.setCode(ResultCode.SUCCESS.getCode());
        restResult.setMessage(msg);
        return restResult;
    }

    protected HttpResult<String> responseOK(String data,String msg) {
        HttpResult<String> restResult = new HttpResult<>();
        restResult.setSuccess(true);
        restResult.setData( data );
        restResult.setMessage( msg );
        restResult.setCode(ResultCode.SUCCESS.getCode());
        return restResult;
    }

    /**
     * 默认失败返回, 不带参数
     *
     * @return
     */
    protected <T> HttpResult<T> responseFail() {
        return responseFail(ResultCode.SYSTEM_ERROR.getCode());
    }

    protected <T> HttpResult<T> responseFail( Integer code ) {
        return responseFail( code ,"");
    }

    /**
     * 默认失败返回, 带信息
     *
     * @param message
     * @return
     */
    protected <T> HttpResult<T> responseFail(String message) {
        return responseFail(ResultCode.SYSTEM_ERROR.getCode(), message);
    }

    /**
     * 失败返回
     *
     * @param code    错误Code
     * @param message 若为null，则使用Code对应的默认信息
     */
    protected <T> HttpResult<T> responseFail(Integer code, String message) {
        HttpResult<T> restResult = new HttpResult<T>();
        restResult.setSuccess(false);
        restResult.setCode(code );
        restResult.setMessage(message);
        return restResult;
    }
}